<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>业务处理-普通入库-编辑普通入库</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a href="<%=basePath%>pickingReturnContrlooer/selectByWhere.do">普通入库</a>
					<i class="icon-double-right"></i>
					<a>编辑普通入库</a>
				</div>

				<div class="btn rightBtn">
					<input type="button" value="新增物品" onclick="toAddPurchaseRequestItem();" class="btn btn1"></input>
					<input type="button" value="修改" onclick="saveAddPurchaseRequest();" class="btn btn2"></input>
					<input type="button" value="返回" onclick="location.href='<%=basePath%>generalStorage/selectByWhere.do'" class="btn btn1"></input>
					<input type="button" value="刷新" onclick="location.reload();" class="btn btn2"></input>
				</div>
				<form id="generalStorageForm" class="form">
					<input type="hidden" value="${gs1.generalstorageid }" name="generalstorageid"></input> <input type="hidden" value="${gs1.lastmodifiedid }" name="lastmodifiedid"></input>
					<div id="requisitionForm" class="box">
						<p>
							<label> 入库单号</label>
							<input type="text" readonly class="form-control" value="${gs1.warehousenumber}"></input>

							<label> 入库人</label>
							<input type="text" readonly class="form-control" value="${purchaser.loginName }"></input>

							<label> 类型</label>
							<select name="materialMaterial2" class="form-control select" id="materialMaterial2" onchange="gradeChange()">
								<option value="IMPA物料" <c:if test="${list2[0].IntemInformation.materialmaterial=='IMPA物料'}">selected</c:if>>IMPA物料</option>
								<option value="普通物料" <c:if test="${list2[0].IntemInformation.materialmaterial=='普通物料'}">selected</c:if>>普通物料</option>
							</select>

							<label> 状态</label>
							<select name="status" class="form-control select">
								<option value="0" <c:if test="${gs1.status=='0'}">selected</c:if>>未提交</option>
								<option value="1" <c:if test="${gs1.status=='1'}">selected</c:if>>已提交</option>
							</select>

						</p>
						<p>
							<label> 仓库</label>
							<select name="warehouseid" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }" <c:if test="${gs1.warehouseid==warehouse.warehousemanagementid}">selected</c:if>>${warehouse.warehousename }</option>
								</c:forEach>
							</select>

							<label>供应商</label>
							<select name="supplierID" onchange="gradeChange()" class="form-control select">
								<c:forEach items="${supplierList }" var="supplier">
									<option value="${supplier.supplierid }" <c:if test="${gs1.supplierID==supplier.supplierid}">selected</c:if>
										>${supplier.chinesename }</option>
								</c:forEach>
							</select>

							<label>说明</label>
							<input type="text" class="form-control" name="storageinstructions" value="${gs1.storageinstructions}"></input>

						</p>

						<p>
							<label>备注</label>
							<input type="text" class="form-control doubleWidth" name="remarks" value="${gs1.remarks}"></input>

						</p>
					</div>
					<div class="tableBox box">
						<table class="table table-bordered table_text_center">
							<thead>
								<tr>
									<th>代码</th>
									<th>物品名称</th>
									<th>物品条码</th>
									<th>品牌</th>
									<th>库存</th>
									<th>过期时间</th>
									<th>采购数量</th>
									<th>规格</th>
									<th>数量</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="purchaseRequestItemTbody">
								<c:forEach items="${list2 }" var="item" varStatus="i">
									<tr>
										<td>${item.IntemInformation.itemcode }</td>
										<td>${item.IntemInformation.chinesename }</td>
										<td>${item.IntemInformation.articlebarcode }</td>
										<td>${item.IntemInformation.brand }</td>

										<td>${item.IntemInformation.currentstock }</td>
										<td>
											<input type='text' name='expiryDate' readonly onclick='WdatePicker({skin:"whyGreen",dateFmt:"yyyy-MM-dd"})' value="<fmt:formatDate value='${item.GeneralStorage_itemInformation.expiryDate }' type='date' />" class='form-control' />

										</td>

										<td><input name='itemNumber2' maxlength='8' oninput='calculations(this)' type='text' value="${item.GeneralStorage_itemInformation.storagequantity}" class='form-control' /></td>
										<td>
											<select oninput='calculations2(this)' class='form-control select'>
												<option value='${item.IntemInformation.salesspecifications }'>${item.IntemInformation.salesspecifications }${item.IntemInformation.salesunit }</option>
												<option value='${item.IntemInformation.purchasespecifications }'>${item.IntemInformation.purchasespecifications } ${item.IntemInformation.salesunit }/${item.IntemInformation.purchasingunit }</option>
											</select>
										</td>

										<td>
											<input type="hidden" name="generalStorageItemInformationId" value="${item.GeneralStorage_itemInformation.id }" />
											<input type='text' name='itemNumber' onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);' value="${item.GeneralStorage_itemInformation.storagequantity}" class='form-control' />
										</td>
										<td>
											<a onclick='deleteItem(this);' class='btn btn-xs btn_danger'>删除</a> <input type="hidden" name='itemIds' value='${item.IntemInformation.inteminformationid}' /></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
					<div class="box updateBox ">
						<p>
							<label>创建人</label>
							<input type="text" class="form-control doubleWidth" readonly value="${createPerson.loginName } "></input>
							<label>创建时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${gs1.createdate  }' pattern=" yyyy-MM-dd HH:mm:ss " /> "></input>
							<br>
							<label>最后提交人</label>
							<input type="text" class="form-control doubleWidth" readonly value="${lastModified.loginName } "></input>
							<label>最后提交时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${gs1.lastmodifiedtime  }' pattern=" yyyy-MM-dd HH:mm:ss " />"></input>
						</p>
					</div>
				</form>
			</div>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
			<jsp:include page="/jsp/storage/normalStorage/addPurchaseRequestItem.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
		<script src="<%=basePath%>My97DatePickers/WdatePicker.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			function gradeChange() {
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:selected").val());
				$('#purchaseRequestItemTbody').empty();
			}

			$(function() {
				// 任何需要执行的js特效 
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:selected").val());
			});

			function addToHasAddTable(item) {
				var html = "<tr>";
				html += "<td>" + item.itemcode + "</td>";
				html += "<td>" + item.chinesename + "</td>";
				html += "<td>" + item.articlebarcode + "</td>";
				html += "<td>" + item.brand + "</td>";

				html += "<td>" + item.currentstock + "</td>";
				html += "<td><input type='text' name='expiryDate' readonly onclick='WdatePicker({skin:\"whyGreen\",dateFmt:\"yyyy-MM-dd\"})' class='form-control'/></td>";

				html += "<td><input name='itemNumber2' maxlength='8'  oninput='calculations(this)' type='text' class='form-control'/></td>";
				html += "<td><select oninput='calculations2(this)' class='form-control select'>" +
					"<option value='" + item.salesspecifications + "'>" + item.salesspecifications + "" + item.salesunit + "</option>" +
					"<option value='" + item.purchasespecifications + "'>" + item.purchasespecifications + "" + item.salesunit + "/" + item.purchasingunit + "</option>" +
					"</select></td>";

				html += "<td><input type='text' name='itemNumber' onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);' class='form-control'/></td>";
				html += "<td><a onclick='deleteItem(this);' class='btn btn-xs btn_danger'>删除</a>";
				html += "<input type='hidden' name='itemIds' value='" + item.inteminformationid + "' /></td>";
				html += "</tr>";
				$("#purchaseRequestItemTbody").append(html);
			}
			/* 输入采购数量自动计算数量 */
			function calculations(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).val().replace(/\D/g, '')) * ($(obj).parent().next().find("option:selected").val()));
			}
			/* 选择规格下拉框在自动计算数量 */
			function calculations2(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).parent().parent().find("input[name='itemNumber2']").val().replace(/\D/g, '')) * ($(obj).find("option:selected").val()));
			}

			function deleteItem(obj) {
				/* var dd=$(obj).parent().parent().find("[name='generalStorageItemInformationId']").val();
				alert(dd); */
				$(obj).parent().parent().remove();
			}

			function validateForm() {
				var length = $("#purchaseRequestItemTbody [name='itemIds']").length;
				if(!length > 0) {
					Prompt.alert("提示", "物品种类数量必须大于0！")
					return false;
				}
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '' || num <= 0) {
						Prompt.alert("提示", "物品采购数量必须大于0！")
						return false;
					}
				}
				/* 检查日期是否填写 */
				var arr = $("#purchaseRequestItemTbody [name='expiryDate']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '') {
						Prompt.alert("提示", "必须填写日期！")
						return false;
					}
				}
				return true;
			}

			function saveAddPurchaseRequest() {
				if(!validateForm()) return;
				$.ajax({
					url: "<%=basePath%>generalStorage/update.do",
					type: "post",
					async: false,
					dataType: "json",
					data: $("#generalStorageForm").serialize(),
					success: function(data) {
						if(data.result == "success") {
							Prompt.alert("提示", "采购申请保存成功！", function() {
								location.href = "<%=basePath%>generalStorage/selectByWhere.do";
							});
						} else {
							Prompt.alert("提示", "采购申请保存失败！");
						}
					},
					error: function() {
						Prompt.alert("提示", "采购申请保存失败！");
					}
				});
			}
		</script>
	</body>

</html>